package com.ludaxu.zhbj.fragment;

import java.util.ArrayList;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.ludaxu.zhbj.R;
import com.ludaxu.zhbj.base.BasePager;
import com.ludaxu.zhbj.base.impl.GovAffairsPager;
import com.ludaxu.zhbj.base.impl.HomePager;
import com.ludaxu.zhbj.base.impl.NewsCenterPager;
import com.ludaxu.zhbj.base.impl.SettingPager;
import com.ludaxu.zhbj.base.impl.SmartServicePager;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

/**
 * 主页内容
 * 
 * @author ludaxu
 * 
 */
public class ContentFragment extends BaseFragment {
	@ViewInject(R.id.rg_group)
	private RadioGroup rgGroup;

	@ViewInject(R.id.vp_content)
	private ViewPager mViewPager;
	private ArrayList<BasePager> mPagerList;

	@Override
	public View initViews() {
		View view = View.inflate(mActivity, R.layout.fragment_content, null);
		// rgGroup = (RadioGroup) view.findViewById(R.id.rg_group);
		ViewUtils.inject(this, view);
		return view;
	}

	@Override
	public void initData() {
		// 父类中该方法什么都没有做,去掉下面的语句
		// super.initData();
		// 默认勾选首页
		rgGroup.check(R.id.rb_home);
		// 初始化5个页面
		mPagerList = new ArrayList<BasePager>();
		// for (int i = 0; i < 5; i++) {
		// BasePager pager = new BasePager(mActivity);
		// mPagerList.add(pager);
		// }

		mPagerList.add(new HomePager(mActivity));
		mPagerList.add(new NewsCenterPager(mActivity));
		mPagerList.add(new SmartServicePager(mActivity));
		mPagerList.add(new GovAffairsPager(mActivity));
		mPagerList.add(new SettingPager(mActivity));

		mViewPager.setAdapter(new ContentAdapter());
		// 设置监听rgGroup的选择事件,切换fragment
		rgGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rb_home:
					// 设置当前页面
					// mViewPager.setCurrentItem(0);
					// 去掉页面的平滑滑动效果
					mViewPager.setCurrentItem(0, false);
					break;
				case R.id.rb_news:
					// 设置当前页面
					mViewPager.setCurrentItem(1);
					break;
				case R.id.rb_smart:
					// 设置当前页面
					mViewPager.setCurrentItem(2);
					break;
				case R.id.rb_gov:
					// 设置当前页面
					mViewPager.setCurrentItem(3);
					break;
				case R.id.rb_setting:
					// 设置当前页面
					mViewPager.setCurrentItem(4);
					break;

				default:
					break;
				}
			}
		});
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				// 获取当前被选中的页面,初始化页面数据
				mPagerList.get(position).initData();

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
		// 加载第一个页面的时候,手动初始化第一个页面的数据
		mPagerList.get(0).initData();

	}

	class ContentAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return mPagerList.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			BasePager pager = mPagerList.get(position);
			// 不能在此处初始化数据,否则预加载下一个页面
			// 会影响逻辑,或者流量的问题
			// pager.initData();// 初始化数据...
			container.addView(pager.mRootView);
			return pager.mRootView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}
	}

	/**
	 * 获取新闻中心页面
	 * 
	 * @return
	 */
	public NewsCenterPager getNewsCenterPager() {
		return (NewsCenterPager) mPagerList.get(1);
	}

}
